package cn.hchaojie.daily.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.hchaojie.daily.dao.DailyDao;
import cn.hchaojie.daily.dao.impl.DailyDaoImpl;
import cn.hchaojie.daily.entity.Daily;

@WebServlet("/dailies/list")
public class DailyListServlet extends HttpServlet {
	private DailyDao dao = new DailyDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer page = Integer.valueOf(req.getParameter("page"));
		Integer size = Integer.valueOf(req.getParameter("limit"));
		
		List<Daily> dailies = dao.getDailies(page, size);
		int count = dao.count();
		
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("count", count);
		map.put("data", dailies);
		map.put("message", "请求成功");
		
		resp.setContentType("application/json;charset=utf-8");
		
		JSON.writeJSONString(resp.getOutputStream(), map);
	}
}
